#pragma once

namespace zzz {
//for automatically destroy
template<typename T>
class SingletonDestroyer
{
public:
  SingletonDestroyer():obj(0){}
  ~SingletonDestroyer(){delete obj;}
  T *obj;
};

template<typename T>
class Singleton 
{
public:
  static T& Instance()
  {
    if (instance_ == 0) 
    {
      instance_ = new T;
      destroyer_.obj=instance_;
    }
    return *instance_;
  }
protected:
  Singleton()
  {
    instance_=0;
  }
private:
  static T* instance_;
  static SingletonDestroyer<T> destroyer_;
};

template<typename T>
T* Singleton<T>::instance_ = 0;
template<typename T>
SingletonDestroyer<T> Singleton<T>::destroyer_;
};  // namespace zzz
